################################################################################
# Get the default date for all defaulted loans
################################################################################

use strict;
use File::Find;


#Run this SQL to generate the list of IDs of all defaulted loans

#--Get comma separated lists of defaulted loan ids, 100 at a time
#select
#  case when idGroup <> 1 then ',' else null end ||
#  to_string2(cast(collect(to_char(id) order by id) as varchar2_ntt), ',') ||',' defaultedLoans
#from
#(
#  select id, ceil(row_number() over (order by id) / 100) idGroup
#  from loan
#  where status = 'defaulted'
#  order by id
#)
#group by idGroup
#order by idGroup;


#First group of loans:
#my @loanIDs = (
#  103,129,130,131,133,142,143,145,146,151,171,172,173,248,249,392,395,700,810,1110,1111,1112,1113,1114,1115,1116,1189,1192,1194,1195,1196,1197,1265,1266,1267,1268,1269,1270,1346,1449,1450,1451,1452,1453,1454,1509,1511,1512,1513,1515,1517,1518,1519,1520,1521,1594,1595,1596,1597,1598,1599,1600,1601,1602,1603,1610,1664,1672,1676,1704,1705,1706,1707,1708,1782,1783,1784,1786,1787,1789,1790,1914,1916,1917,1918,1919,1921,1922,1923,1924,1925,1926,1927,1928,1929,1930,1931,1932,1934,1936,
#  ,1961,1962,1963,1969,1970,1971,1972,1973,1974,1983,2027,2028,2029,2030,2031,2032,2033,2034,2035,2036,2037,2038,2039,2040,2041,2042,2043,2044,2045,2064,2065,2066,2067,2068,2098,2099,2100,2101,2102,2103,2104,2105,2106,2107,2108,2109,2110,2111,2112,2113,2114,2130,2131,2145,2146,2147,2213,2235,2236,2237,2280,2307,2308,2309,2310,2311,2312,2313,2314,2315,2316,2317,2318,2319,2320,2321,2322,2323,2325,2327,2328,2329,2333,2334,2335,2337,2338,2339,2340,2341,2387,2388,2389,2394,2395,2396,2397,2398,2399,2400,
#  ,2401,2402,2403,2549,2550,2552,2554,2556,2558,2559,2560,2561,2563,2565,2570,2571,2572,2574,2575,2576,2578,2580,2720,2724,2766,2768,2769,2770,2771,2772,2773,2774,2775,2776,2778,2779,2783,2785,2786,2787,2839,2848,2849,2850,2908,2909,2910,2911,2913,2914,3024,3025,3026,3028,3029,3031,3033,3035,3037,3038,3063,3081,3083,3084,3086,3087,3089,3091,3093,3102,3103,3195,3196,3197,3229,3230,3231,3232,3233,3234,3235,3304,3305,3306,3307,3308,3309,3310,3311,3312,3313,3314,3316,3317,3318,3319,3320,3321,3322,3323,
#  ,3324,3325,3326,3327,3328,3329,3330,3331,3332,3333,3334,3335,3336,3337,3338,3340,3341,3342,3343,3344,3345,3346,3347,3379,3380,3384,3389,3390,3391,3411,3474,3513,3514,3516,3517,3518,3597,3599,3601,3605,3606,3607,3612,3613,3614,3620,3622,3625,3754,3755,3757,3785,3786,3795,3796,3797,3798,3799,3800,3801,3802,3803,3804,3805,3806,3807,3808,3809,3810,3811,3812,3813,3814,3815,3816,3817,3818,3819,3820,3821,3850,3853,3854,3855,3857,3858,3859,3860,3862,3863,3864,3866,3867,3876,3877,3880,3882,3884,3886,3887,
#  ,3888,3889,3891,3892,3893,3894,3896,3898,3900,3901,3902,3904,3905,3911,3913,3954,3955,3956,3957,3958,3959,3960,3961,3962,3966,3967,3968,3969,3985,3986,4096,4097,4098,4099,4100,4101,4102,4103,4104,4105,4106,4107,4108,4109,4111,4151,4152,4153,4154,4155,4156,4157,4158,4159,4263,4264,4265,4266,4267,4268,4269,4271,4272,4273,4275,4277,4278,4279,4280,4311,4312,4313,4314,4315,4316,4317,4318,4381,4383,4384,4385,4387,4397,4398,4399,4401,4402,4406,4407,4411,4414,4415,4417,4424,4434,4437,4439,4441,4443,4446,
#  ,4448,4528,4566,4616,4617,4618,4620,4622,4624,4625,4627,4751,4752,4759,4760,4761,4762,4763,4764,4766,4767,4773,4774,4778,4781,4782,4785,4786,4787,4790,4792,4795,4842,4847,4848,4849,4850,4851,4854,4857,4859,4860,4863,4864,4865,4911,4925,4931,4934,4937,4940,4949,4954,4958,4960,4965,4969,4974,4977,4981,4990,4992,4993,4994,5002,5004,5005,5006,5008,5011,5013,5015,5017,5019,5022,5024,5028,5031,5032,5033,5143,5150,5151,5154,5160,5167,5169,5218,5219,5220,5221,5222,5223,5224,5225,5226,5227,5229,5230,5232,
#  ,5234,5293,5294,5295,5296,5297,5298,5300,5301,5302,5303,5304,5305,5306,5307,5308,5309,5310,5311,5312,5313,5314,5315,5316,5409,5471,5473,5474,5477,5480,5481,5483,5485,5489,5491,5494,5496,5499,5501,5504,5511,5515,5517,5518,5520,5522,5562,5564,5566,5577,5578,5579,5587,5589,5597,5608,5621,5632,5647,5710,5713,5714,5717,5720,5723,5732,5758,5761,5763,5768,5769,5774,5776,5809,5810,5812,5814,5817,5818,5822,5825,5830,5832,5836,5841,5845,5847,5850,5857,5859,5863,5865,5871,5873,5886,5902,5909,5922,5935,5937,
#  ,5944,5945,5946,5952,5968,5977,5983,5988,5991,5995,5997,5999,6000,6003,6006,6008,6010,6020,6024,6078,6079,6081,6083,6084,6085,6087,6088,6090,6091,6093,6094,6095,6166,6172,6173,6174,6175,6176,6177,6178,6180,6189,6190,6196,6199,6219,6223,6355,6356,6358,6359,6361,6362,6364,6365,6366,6367,6368,6370,6371,6372,6374,6375,6377,6378,6380,6383,6384,6386,6388,6389,6391,6392,6394,6395,6397,6398,6484,6487,6488,6489,6490,6491,6492,6521,6538,6599,6600,6602,6604,6608,6609,6610,6612,6613,6614,6616,6623,6624,6625,
#  ,6626,6638,6639,6640,6641,6642,6643,6644,6647,6649,6650,6653,6667,6668,6669,6671,6672,6674,6675,6676,6678,6680,6681,6765,6778,6779,6780,6781,6832,6833,6834,6835,6836,6837,6838,6839,6840,6841,6842,6843,6844,6845,6846,6876,6877,6878,6883,6884,6885,6886,6887,6888,6889,6890,6891,6892,6893,6894,6895,6896,6897,6898,6899,6900,6901,6902,6903,6904,6923,6924,6925,6926,6927,6928,6929,6930,6931,6932,6933,6971,6972,6973,7038,7053,7054,7055,7056,7057,7058,7061,7074,7075,7076,7077,7078,7079,7080,7081,7082,7083,
#  ,7084,7085,7086,7088,7089,7119,7121,7122,7123,7124,7125,7127,7128,7130,7166,7167,7223,7225,7226,7227,7228,7229,7230,7231,7232,7234,7326,7423,7495,7496,7497,7498,7499,7500,7501,7502,7504,7507,7509,7510,7512,7514,7562,7598,7599,7600,7602,7603,7604,7605,7606,7607,7608,7609,7669,7670,7671,7673,7674,7675,7676,7678,7679,7680,7682,7683,7684,7685,7687,7688,7690,7691,7693,7694,7695,7697,7698,7700,7701,7702,7703,7706,7707,7710,7711,7712,7713,7714,7715,7718,7719,7720,7721,7722,7760,7761,7762,7763,7764,7765,
#  ,7766,7767,7768,7769,7770,7771,7772,7773,7774,7775,7776,7777,7778,7779,7780,7781,7782,7783,7784,7785,7786,7787,7788,7789,7790,7791,7792,7793,7795,7796,7797,7798,7800,7802,7803,7805,7806,7808,7809,7811,7813,7817,7818,7820,7821,7823,7824,7825,7827,7828,7829,7831,7832,7833,7834,7835,7836,7837,7838,7839,7845,7847,7848,7849,7850,7852,7853,7854,7856,7857,7858,7860,7861,7862,7864,7865,7866,7867,7868,7869,7870,7874,7875,7876,7877,7878,7879,7880,7881,7882,7883,7884,7885,7886,7887,7888,7889,7890,7891,7892,
#  ,7893,7894,7895,7896,7897,7898,7901,7906,7907,7911,7912,7913,7914,7917,7918,7920,7925,7927,7929,7930,7933,7934,7935,7937,7939,7941,7942,7945,7947,7949,7951,7953,8046,8048,8051,8052,8054,8055,8057,8059,8060,8066,8069,8070,8072,8073,8077,8080,8084,8102,8104,8105,8110,8112,8116,8120,8124,8134,8163,8166,8170,8171,8176,8181,8183,8184,8186,8187,8192,8194,8197,8199,8200,8202,8204,8207,8210,8212,8213,8216,8218,8224,8231,8233,8237,8238,8242,8245,8247,8251,8252,8257,8259,8265,8275,8278,8283,8285,8290,8738,
#  ,8739,8740,8741,8742,8743,8744,8745,8788,8790,8791,8792,8795,8796,8798,8802,8804,8810,8814,8817,8825,8834,8838,8839,8840,8841,9037,9038,9039,9040,9041,9042,9043,9334,9335,9336,9343,9344,9345,9346,9347,9348,9349,9350,9351,9352,9353,9354,9355,9357,9358,9359,9360,9361,9362,9363,9378,9379,9380,9381,9382,9383,9384,9385,9386,9387,9388,9389,9390,9392,9393,9395,9396,9398,9399,9402,9403,9404,9406,9407,9408,9409,9410,9411,9412,9413,9426,9427,9429,9430,9431,9433,9434,9437,9439,9441,9443,9444,9445,9446,9447,
#  ,9448,9475,9476,9477,9478,9480,9481,9485,9491,9530,9531,9533,9539,9544,9549,9611,9612,9613,9614,9615,9616,9617,9618,9619,9621,9623,9625,9626,9628,9631,9633,9634,9635,9638,9639,9640,9642,9644,9645,9646,9647,9648,9649,9650,9651,9652,9708,9709,9710,9711,9712,9713,9714,9715,9716,9717,9718,9719,9720,9722,9723,9724,9725,9795,9857,9892,9903,9905,9969,9971,9972,9973,10071,10072,10073,10074,10075,10076,10077,10079,10082,10084,10086,10136,10137,10139,10140,10141,10142,10143,10147,10162,10164,10167,10168,10170,10176,10177,10178,10179,
#  ,10180,10181,10184,10191,10195,10384,10387,10388,10391,10393,10427,10430,10432,10449,10455,10456,10457,10458,10459,10460,10461,10462,10463,10464,10504,10506,10621,10622,10623,10625,10626,10632,10634,10639,10654,10656,10657,10659,10661,10665,10746,10758,10759,10760,10761,10763,10765,10766,10767,10792,10793,10796,10798,10799,10802,10805,10811,10813,10815,10820,10829,10965,10966,10967,10968,10969,10970,10971,10972,11018,11019,11020,11021,11022,11033,11089,11091,11092,11093,11094,11096,11097,11099,11100,11101,11105,11107,11112,11113,11119,11125,11128,11131,11530,11540,11757,11861,11862,11863,11880,
#  ,11883,11884,11885,11887,11889,11890,11891,11893,12004,12006,12009,12014,12019,12081,12155,12162,12177,12187,12287,12322,12366,12399,12400,12402,12404,12412,12415,12417,12457,12459,12460,12462,12463,12467,12468,12469,12470,12471,12473,12474,12476,12477,12479,12480,12514,12518,12519,12564,12565,12566,12591,12636,12637,12662,12664,12665,12667,12669,12670,12681,12682,12683,12686,12687,12777,12778,12779,12795,12796,12872,12873,12875,12876,12877,12926,12928,12929,12930,12933,12934,12935,12937,12940,12943,12945,12947,12952,13086,13087,13088,13089,13090,13091,13092,13093,13094,13095,13096,13098,13099,
#  ,13100,13101,13122,13123,13181,13182,13194,13195,13196,13197,13198,13199,13200,13201,13202,13203,13204,13205,13206,13207,13208,13209,13308,13315,13316,13345,13392,13395,13397,13400,13505,13558,13560,13591,13592,13593,13594,13595,13599,13603,13604,13606,13803,13858,13859,13860,13863,13864,13866,13867,13869,13872,13873,13874,13875,13876,13880,13881,13889,13939,13942,13988,14027,14047,14052,14072,14076,14078,14079,14096,14101,14105,14106,14107,14130,14131,14133,14134,14136,14137,14138,14170,14171,14172,14173,14175,14177,14179,14180,14181,14182,14183,14184,14195,14199,14202,14204,14205,14206,14207,
#  ,14284,14285,14291,14330,14331,14338,14389,14485,14487,14488,14492,14513,14514,14530,14531,14533,14535,14536,14537,14538,14539,14540,14541,14542,14543,14544,14546,14547,14548,14549,14550,14552,14553,14555,14560,14565,14632,14634,14635,14636,14637,14639,14641,14663,14665,14666,14743,14772,14821,14825,14826,14850,14853,14858,14861,14863,14952,14953,14955,15032,15033,15049,15050,15059,15261,15263,15271,15278,15282,15283,15287,15288,15333,15334,15335,15338,15343,15347,15421,15426,15427,15428,15459,15566,15570,15572,15573,15575,15606,15607,15608,15609,15638,15639,15640,15641,15642,15643,15644,15645,
#  ,15649,15650,15651,15667,15668,15669,15671,15697,15698,15715,15729,15744,15746,15752,15754,15760,15761,15786,15792,15820,15821,15822,15823,15836,15837,15838,15839,15842,16091,16092,16094,16095,16097,16256,16258,16259,16313,16335,16337,16340,16344,16413,16418,16544,16545,16546,16547,16581,16583,16589,16592,16593,16612,16613,16614,16615,16616,16789,16791,16808,16809,16810,16837,16920,16928,16951,16953,16954,16961,16963,16969,16972,17202,17203,17206,17208,17300,17301,17308,17315,17316,17319,17320,17321,17343,17345,17348,17356,17359,17360,17361,17362,17363,17364,17365,17367,17509,17514,17516,17517,
#  ,17523,17611,17652,17653,17657,17779,17800,17802,17810,17819,17820,17822,17823,17825,17831,17832,17833,17834,17835,17836,17853,17886,17934,17936,17937,17938,17939,17941,17942,17943,17945,17946,17947,17955,17958,17961,17965,17970,17982,17996,17997,18062,18070,18086,18133,18154,18162,18166,18169,18170,18256,18257,18258,18358,18359,18360,18373,18374,18375,18377,18378,18379,18411,18416,18417,18420,18421,18469,18472,18561,18567,18570,18577,18583,18662,18669,18680,18681,18682,18684,18686,18688,18699,18939,18940,18941,18943,18946,18947,18948,18953,18954,18958,18959,19281,19325,19329,19335,19342,19347,
#  ,19566,19572,19577,19579,19635,19654,19770,19771,19772,19773,19774,19775,19777,19939,19951,19960,19962,19963,19965,19966,19967,19970,19972,19973,19975,20317,20318,20319,20320,20322,20323,20324,20325,20326,20329,20709,20716,20719,20724,20728,20733,20735,20737,20738,20739,20740,20741,20742,20743,20746,20747,20749,20751,20770,20869,20870,20872,20873,20876,20884,20924,20925,20926,21015,21154,21155,21156,21157,21158,21166,21176,21181,21189,21190,21193,21194,21195,21199,21200,21201,21202,21204,21206,21207,21208,21367,21370,21373,21379,21433,21434,21435,21436,21438,21497,21500,21501,21605,21660,21754,
#  ,21757,21758,21766,21982,21983,21984,22035,22037,22038,22454,22465,22468,22472,22473,22475,22476,22478,22479,22483,22529,22532,22535,22537,22538,22556,22558,22563,22575,22576,22577,22578,22579,22580,22581,22582,22583,22584,22585,22600,22609,22642,22643,22644,22647,22648,22712,22759,22760,22761,22762,22763,22844,22849,22856,22861,22868,22942,22946,22974,23052,23053,23090,23092,23095,23098,23143,23237,23493,23499,23501,23502,23503,23504,23507,23508,23511,23518,23520,23525,23528,23531,23533,23535,23536,23537,23608,23614,23616,23620,23621,23622,23623,23628,23629,23636,23637,23638,23641,23658,23659,
#  ,23660,23662,23663,23665,23666,23668,23672,23676,23680,23682,23807,23819,23878,23880,23882,23883,23884,23887,23889,23890,23892,23893,23894,23895,23897,23898,23900,23903,23904,23907,23909,23918,23922,23924,23926,23927,23929,23930,23933,23934,23952,23962,23964,23965,23966,23983,23984,23985,23986,23987,23991,23992,23993,23995,23996,24289,24291,24357,24386,24388,24389,24420,24424,24425,24432,24435,24546,24549,24552,24555,24588,24589,24590,24591,24602,24615,24621,24626,24629,24918,24928,24934,24974,24977,24985,25109,25116,25118,25122,25190,25219,25251,25299,25337,25425,25427,25428,25434,25436,25438,
#  ,25441,25442,25448,25449,25457,25459,25464,25466,25471,25481,25482,25483,25488,25489,25552,25789,25806,25819,25983,25988,25990,25994,25999,26005,26015,26020,26030,26032,26034,26038,26042,26044,26109,26111,26125,26141,26142,26143,26152,26159,26173,26228,26230,26260,26261,26262,26263,26264,26265,26288,26289,26290,26291,26292,26293,26294,26295,26296,26297,26298,26299,26300,26301,26302,26303,26304,26305,26306,26307,26311,26312,26313,26370,26371,26400,26413,26415,26416,26551,26552,26555,26565,26615,26616,26617,26717,26722,26726,26746,26748,26750,26786,26798,26859,26868,26869,26928,26930,26932,26933,
#  ,26934,26953,26954,26955,26972,27042,27058,27060,27063,27065,27066,27068,27069,27070,27071,27072,27074,27075,27077,27079,27081,27083,27085,27087,27088,27089,27096,27134,27135,27136,27137,27138,27433,27533,27541,27543,27547,27551,27553,27575,27606,27611,27615,27625,27627,27652,27654,27657,27671,27673,27738,27739,27740,27741,27742,27743,27813,27814,27815,27816,27924,27936,27962,27963,27964,27965,27970,27976,27985,27998,28002,28009,28010,28011,28012,28013,28014,28015,28016,28017,28018,28062,28148,28150,28151,28152,28155,28163,28172,28197,28208,28601,28603,28604,28605,28732,28736,28830,28836,28839,
#  ,28870,28872,28873,28875,29028,29030,29033,29034,29042,29159,29160,29161,29162,29217,29218,29219,29220,29221,29222,29223,29224,29236,29237,29238,29239,29244,29324,29326,29328,29329,29330,29331,29332,29333,29335,29336,29942,30129,30130,30333,30334,30335,30336,30337,30338,30339,30340,30342,30343,30344,30345,30346,30348,30349,30735,30889,30890,30892,30895,30912,31143,31145,31147,31149,31151,31162,31163,31164,31165,31166,31715,31719,31723,31725,31729,32205,32353,32354,32355,32381,32398,32399,32400,32401,32405,32409,32414,32416,32417,32418,32421,32422,32423,32772,32774,32777,33770,33817,33820,33825,
#  ,33967,33980,34228,34235,34238,34241,34244,34247,34256,34257,34267,34269,34272,34273,34274,34276,34277,34283,34287,34289,34294,34304,34578,34579,34580,34599,34604,34612,34784,34785,34786,34787,34788,34789,34790,34791,34874,34875,34887,34888,34889,34898,34899,34900,34901,34902,35117,35118,35119,35120,35236,35237,35245,35246,35247,35469,35484,35530,35531,35559,35560,35561,35562,35563,35564,35565,35566,35567,35568,35569,35570,35571,35572,35573,35580,35582,35584,35585,35587,35588,35589,35629,35822,35823,35826,35829,35875,35879,36101,36107,36109,36111,36112,36115,36116,36117,36118,36120,36122,36125,
#  ,36126,36127,36128,36135,36259,36265,36405,36423,36424,36426,36428,36430,36431,36447,36451,36452,36457,36458,36490,36495,36511,36527,36539,36543,36551,36579,36600,36618,36642,36647,36648,36677,36679,36680,36984,36986,36987,36988,36990,36995,36998,37002,37006,37007,37008,37009,37010,37259,37333,37335,37336,37339,37405,37406,37865,38356,38362,38669,38891,38893,38895,38909,38914,38936,38977,39002,39012,39013,39021,39100,39104,39111,39112,39273,39275,39276,39277,39278,39280,39357,39359,39361,39365,39371,39397,39501,39503,39504,39506,39544,39573,39673,39703,39709,39931,40141,40148,40152,40153,40154,
#  ,40155,40477,40478,40479,40576,40602,40605,40609,40610,40613,40615,40619,40621,40761,40765,40768,40769,40771,40779,40783,40786,40941,40951,40952,40953,40954,40960,40964,40965,40966,40968,40972,40973,41181,41190,41196,41203,41205,41210,41213,41216,41218,41222,41224,41228,41232,41564,41565,41567,41574,41586,41599,41626,41996,42310,42312,42313,42315,42316,42317,42318,42319,42320,42321,42322,42323,42457,42484,42572,42573,42575,42576,42578,42579,42581,42583,42584,42598,42715,42717,42718,42719,42720,43073,43223,43225,43227,43228,43230,43231,43232,43235,43237,43240,43243,43248,43249,43272,43274,43280,
#  ,43423,43424,43425,43426,43428,43429,43430,43431,43435,43436,43640,43725,44270,44275,44276,44277,44387,44984,44987,44991,45259,45455,45457,46041,46042,46043,46178,46312,46313,46314,46333,46390,46392,46930,47242,47275,47276,47277,47278,47279,47280,47281,47282,47284,47285,47286,47287,47288,47289,47290,47291,47292,47293,47294,47295,47296,47297,47298,47706,47707,47708,47709,47710,47711,48061,48062,48063,48064,48204,48522,48909,48929,49004,49090,49091,49148,49150,49153,49156,49221,49222,49223,49224,49225,49226,49227,49228,49282,49283,49284,49285,49309,49313,49324,49408,49409,49412,49415,49418,49420,
#  ,49421,49423,49455,49456,49457,49458,49459,49584,49586,49596,49623,49655,49657,49659,49661,49759,49762,49836,49837,49840,49841,49843,49844,49846,49847,49912,49913,49914,49921,50109,50160,50163,50164,50248,50249,50250,50251,50324,50325,50326,50406,50409,50411,50413,50414,50416,50459,50589,50592,50603,50605,50606,50608,50610,50612,50614,50660,50661,50662,50668,50669,50672,50674,50676,50677,50725,50728,50732,50734,51334,51354,51355,51357,51476,51477,51488,51777,51778,51779,51780,51903,51905,51910,51911,51914,51979,52022,52023,52028,52106,52107,52108,52109,52110,52300,52303,52304,52306,52384,52385,
#  ,52443,52444,52469,52472,52475,52483,52485,52504,52509,52523,52577,52579,52581,52724,52725,52726,52727,52728,52739,52741,52742,52744,52768,52769,52770,52771,52773,52774,53182,53183,53184,53187,53190,53286,53287,53288,53289,53302,53304,53306,53768,53936,53993,53994,53995,53996,53997,53998,53999,54000,54001,54205,54418,54419,54420,54421,54422,54424,54736,54737,54738,54739,55167,55169,55171,55465,55466,55467,55511,55512,55513,55637,55638,55640,55645,55646,55656,55789,55790,56428,56429,56430,56565,56568,56571,56576,56578,56579,56991,56993,56994,56996,56997,56999,57001,57003,57536,57537,57538,57539,
#  ,57540,57541,57542,57543,57576,57580,57582,57586,57587,57588,57589,57590,57591,57593,57594,57815,57817,57818,57821,57822,57824,57826,57827,57949,57951,57953,57954,58097,58099,58100,58105,58108,58283,58284,58285,58286,58287,58500,58501,58502,58503,58508,58511,59234,59235,59236,59265,59266,59267,59268,59269,59292,59296,59299,59300,59301,59438,59439,59440,59442,59443,59564,59565,59567,59568,59570,59574,59575,60602,60603,60605,60606,60609,60611,60702,60719,60720,60721,60722,60723,61226,61231,61504,61543,61544,61545,61547,62122,62125,62128,62129,62131,63245,63246,63247,63249,63250,63251,63455,63456,
#  ,63457,63460,63944,63945,63947,63948,63998,63999,64000,64001,64002,64040,64041,64104,64112,64235,64236,64237,64238,64239,64240,64241,64247,64507,64536,64582,64583,64584,64607,64608,64609,64610,65682,65687,65691,65693,65697,65704,65713,66172,66174,66201,66205,66206,66210,66226,66227,66228,66318,66331,66672,66673,66730,66736,66739,66864,66996,66998,67000,67042,67044,67045,67050,67051,67054,67059,67062,67122,67123,67124,67125,67140,67141,67142,67166,67167,67172,67176,67302,67303,67304,67312,67316,67318,67320,67462,68185,68246,68251,68378,68381,68383,68385,68412,68416,69069,69072,69077,69126,69127,
#  ,69193,69194,69204,69275,69281,69285,69286,69288,69293,69294,69298,69301,69443,69444,69447,69457,69508,69509,69517,69519,69522,69667,69668,69670,70288,70289,70290,70400,70402,70408,70453,70454,70456,70457,70458,70464,70581,70583,70594,70610,70611,70615,70617,70886,70887,71140,71142,71143,71146,71374,71375,71376,71377,71378,71404,71407,71409,71412,71413,71414,71416,72664,72686,72691,73043,73046,73603,73928,73929,73931,74268,74582,74589,74593,74734,74735,74736,74748,74762,74763,74767,74810,74814,74828,74866,74871,74872,74916,74931,74937,74950,74965,75234,75236,75315,75776,75778,76425,76430,77206,
#  ,77207,77208,78161,78231,78330,78344,78345,78447,78449,78453,78455,78456,78506,78833,78843,78847,78849,80050,80202,80203,80205,80208,82234
#);


#Second group, these loans don't have a date, we needed to refresh the pages
my @loanIDs = (
    10176,10177,10178,10746,11530,11540,13505,13558,13560,16544,16545,16546,16547,18684,18686,18688,
    18699,22479,22483,23143,24918,24928,24934,24974,25116,25118,25122,25190,25448,25459,25552,27069,
    27077,27079,27083,27088,27089,27096,27433,28151,28155,32205,33770,33817,33820,33825,33967,33980,
    38669,38891,38893,38895,38909,38914,38936,38977,39703,41996,44270,44275,44276,44277,44387,45455,
    45457,4566,46178,46312,46313,46314,46333,46390,46392,46930,47242,49282,49283,49284,49285,49309,
    49313,49324,50589,50592,5143,51903,51905,51910,51911,51914,51979,52577,52579,52581,5409,54205,
    57576,57580,57582,57586,57587,57588,57589,57590,57591,57593,57594,5935,6079,61226,61231,61504,64104,
    64112,64507,64536,6484,65682,65687,65691,65693,65697,65704,65713,69275,69281,69285,69286,69288,
    69293,69294,69298,69301,7061,7085,7089,7326,7423,75234,75236,75315,7562,80050,9544,9549
);


#Convert array to hash
my %defaultDates;

foreach my $loanID (@loanIDs)
{
  $defaultDates{$loanID} = "";
}


#Look for each loan's HTML page, store it in the hash if found
foreach my $loanID (sort(keys %defaultDates))
{
  #find(\&getDefaultDate, "D:/Backups and Exports/Loan HTML/".$loanID.".html");
  find(\&getDefaultDate, "D:/Backups and Exports/Loan HTML for default dates/".$loanID.".html");
}


my @loansWithoutDates;

#Print the hash as a select statement
#A few of these values will be null, we can sort them out manually
foreach my $key (sort(keys %defaultDates))
{
  if($defaultDates{$key} eq "")
  {
    push @loansWithoutDates, $key;
  }
  else
  {
    print "select $key, \'$defaultDates{$key}\' from dual union all \n"; 
  }
}

#Print the loans without a date
print "\n\n\n";
foreach my $loan (sort(@loansWithoutDates))
{
  print $loan."\n";
}




################################################################################
sub getDefaultDate
{
  my $fileName = $_;
  my $defaultDate;
  my $endedDate;

  #Get the loan ID
  my $loanID = $fileName;
  $loanID =~ s/.html//;

  #open the file
  open FILE, "<", $fileName or die $!;
  
  #Loop through the lines
  while (my $line = <FILE>)
  {
    #If the line contains "Defaulted: ", get the text after it, it should be the date
    $_ = $line;
    if ( m/Defaulted: (.*)/i)
    {
      $defaultDate = $1;
    }

    if ( m/Ended: (.*)/i)
    {
      $endedDate = $1;
    }
  }

  #Store the date in the hash map
  #Some loans say "Defaulted:", some say "Ended:".  Choose Defaulted if it exists, else use Ended
  if($defaultDate ne "")
  {
    $defaultDates{$loanID} = $defaultDate;
  }
  else
  {
    $defaultDates{$loanID} = $endedDate;
  }
}


